package nl.sportan.samenloop;

import java.util.ArrayList;
import java.util.List;

import nl.sportan.samenloop.adapter.ProgramAdapter;
import nl.sportan.samenloop.communication.ProgramReader;
import nl.sportan.samenloop.communication.Result;
import nl.sportan.samenloop.model.ProgramItem;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class CinemaActivity extends ListActivity {

	private Handler handler;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		handler = new Handler();

		setContentView(R.layout.cinema_layout);
		
		initReader();

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
	}

	private void initReader() {
		ProgramReader programReader = new ProgramReader(
				getString(R.string.program_cinema_url));
		programReader.setResultHandler(new Result<ArrayList<ProgramItem>>() {

			public void onSucces(ArrayList<ProgramItem> result) {
				handler.post(new ListUpdater(result));

			}

			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub

			}
		});

		Thread t = new Thread(programReader);
		t.start();
	}

	private class ListUpdater implements Runnable {

		private List<ProgramItem> items;

		public ListUpdater(List<ProgramItem> items) {
			this.items = items;
		}

		public void run() {
			setListAdapter(new ProgramAdapter(CinemaActivity.this, items));

		}

	}

}
